<?php


namespace App\Zesow\Cache\Impl;

use Illuminate\Support\Facades\Cache;
use App\Services\Business\Interfaces\MyCategoryServiceInterface;

class MyCategoryCache
{

    public const KEY_NAME = 'my_category';

    private $myCategoryService;

    public function __construct(MyCategoryServiceInterface $myCategoryService)
    {
        $this->myCategoryService = $myCategoryService;
    }

    public function tree(string $userId)
    {

        if (Cache::has(self::KEY_NAME)) {
            return Cache::get(self::KEY_NAME);
        }
        $mycategories = $this->myCategoryService->tree($userId);
        Cache::put(self::KEY_NAME, $mycategories);
        return $mycategories;


    }

    public function destroy(int $userId): void
    {
        Cache::forget($this->key($userId));
    }

    private function key(int $userId): string
    {
        return self::KEY_NAME . '_' . $userId;
    }

}
